<?php

	error_reporting(1);

	$movtypes	= array("mov","avi","mpg","mpeg","divx","ogm","mp4","mkv","m4v","mp4v");	// What are movie file extensions
	$startdir	= getcwd();

	$forcexml		= false;											// Force creation of XML files, even if already existing
	$forcecover		= false;											// Force creation of cover files, even if already existing
	$repaircover	= false;											// Repair cover symlinks (unlink + new symlink)
	$getsubs		= false;											// Get subtitles for the movies

	while(count($argv) > 2){
		if(substr($argv[1],0,1) == "-"){
			switch(substr($argv[1],1)){
				case "x":	// Force recreate xml files
				case "xml":
					echo "Force re-download of XML files = ON\n";
					$forcexml = true;
					break;
				case "c":
				case "cover":
					echo "Force re-download of cover files = ON\n";
					$forcecover = true;
					break;
				case "rc":
				case "repaircover":
					echo "Force remake of cover symlinks = ON\n";
					$repaircover = true;
					break;
				case "ws":
				case "withsubs":
					echo "Subtitle downloads (in development)\n";
					$getsubs = true;
					break;
				case "md":
				case "makedir":
					echo "Make genre and year directories\n";
					$makedirs = true;
					break;
			}
			array_splice($argv, 1, -1*(count($argv)-2));
		}
	}


	if(isset($argv[1])){ // Directory is given
		$subdirname = $argv[1];

		$imdbnr		= 0;
		$filesize	= 0;
		$xml		= array();
		$xmlfile	= false;
		$cover		= false;
		$subtitles	= false;
		$listavi	= array();
		$listavism	= array();
		$sample		= "";
		$trailer	= "";
		$movcount	= 0;
		$xmlcount	= 0;
		$jpgcount	= 0;
		$srtcount	= 0;
		$nfofile	= "";

		$temp = explode("/",$subdirname);
		$movie = $temp[count($temp)-2];
		$movie   = preg_replace("/\s*?\(.*/","",$subdirname);		// Remove everything after first bracket (year, rip type, ...)

		$subdir = opendir($subdirname);
		while(false !== ($file = readdir($subdir))){
			if(substr($file,0,1) == "."){ continue; }				// No hidden files / dirs
			$ext = strtolower(substr($file, strrpos($file, ".")+1));
			if($ext == ""){ continue; }
			if(in_array($ext, $movtypes)){							// It is a movie
				$movcount++;
				if(strpos($file, "samp") > 0){						// It is a sample
					$sample = $file;
				}elseif(strpos($file, "trailer") > 0){				// It is a trailer
					$trailer = $file;
				}else{												// It is a part of the movie
					array_push($listavism, substr($file, 0, strrpos($file, ".")));
					array_push($listavi, $file);
					if($filesize == 0){								// Get file size of first movie, for subtitle downloading...
						$filesize = filesize($subdirname."/".$file);
					}
				}
			}elseif(strpos($file, ".xml") > 0){						// It is a xml data file
				$xmlfile = true;
				$xmlcount++;
			}elseif($file == "cover.jpg"){							// It is a (folder) cover
				$cover = true;
				$jpgcount++;
			}elseif($ext == "jpg"){									// Other JPG file, consider it to be symlink to cover
				$jpgcount++;
			}elseif(($ext == "srt") or ($ext == "sub")){			// We have subtitles...
				$srtcount++;
				$subtitles = true;
			}elseif($ext == "nfo"){									// We have more info...
				$nfofile = $file;
			}
		}


		if($xmlcount == $movcount){ echo "1"; }
		elseif($xmlcount > 0){ echo "2"; }
		else{ echo "3"; }

		if($cover){ 
			if($jpgcount >= $movcount+1){
				echo "1";
			}else{
				echo "2";
			}
		}else{ 
				echo "3"; 
		}
		if($srtcount > 0){ echo "1"; }else{ echo "3"; }
		if($trailer == ""){ echo "3"; }else{ echo "1"; }


		exit;
	}
?>
